'use strict';

/* Controllers */
  // signin controller
app.controller('SigninFormController', ['$scope', '$http', '$state', function($scope, $http, $state) {
    $scope.user = {};
    if ($.cookie('isLOgin')==="true") {
        $("body").css('background-image','');
        $state.go('app.todoPage');
    }else{
        $("body").css('background-image','url(./tpl/1.jpg)');
        $("body").css('background-repeat','round');
        $("body").css('background-size','cover');
        $("body").css('width','100%');
        $("body").css('height','100%');
    }

    $scope.authError = null;
    $scope.login = function() {
      $scope.authError = null;
      if($scope.user.userName==="" || $scope.user.password===""){
          $scope.authError = '账号或密码不能为空！';
      }else{
          if($scope.user.userName=="computer"){
              $.cookie('userName', '本机');
              $.cookie('userId', 'computer');
              $.cookie('isLOgin', true);
              $.cookie('userIdId', 'computer');
              $("body").css('background-image','');
              $("body").css('background-repeat','');
              $state.go('app.todoPage');
          }else{
              $http.post('/a_yongan/log/newlogin', {userName: $scope.user.userName, password: $scope.user.password})
                  .then(function(response) {
                      if ( !response.data ) {
                          $scope.authError = '账号或密码不正确！';
                      }else{
                          $scope.app.userName=response.data.userName;
                          $scope.app.userId=response.data.userEname;
                          $scope.app.userIdId = response.data.userId;
                          $.cookie('userName', response.data.userName);
                          $.cookie('userId', response.data.userEname);
                          $.cookie('userIdId', response.data.userId);
                          $.cookie('isLOgin', true);
                          if(response.data.userEname!==""){
                              $http.get('/a_yongan/single/power/'+response.data.userEname, null)
                                  .then(function(response1) {
                                      if(!response1.data.isLogin) {
                                          $.cookie('userName', null);
                                          $.cookie('userId', null);
                                          $.cookie('userIdId', null);
                                          $.cookie('isLOgin', false);
                                          $scope.authError = '权限获取出错！';
                                      }else{
                                          if(response1.data.success){
                                              $scope.app.power = response1.data.data;
                                              $("body").css('background-image','');
                                              $("body").css('background-repeat','');
                                              $state.go('app.todoPage');
                                          }else{
                                              $scope.app.power = null;
                                              alert(response.data.error);
                                              $.cookie('userName', null);
                                              $.cookie('userId', null);
                                              $.cookie('isLOgin', false);
                                              $.cookie('userIdId', null);
                                              $scope.authError = '权限获取出错！';
                                          }
                                      }
                                  }, function(x) {
                                      console.log("接口请求出错！");
                                      $scope.authError = '权限获取出错！';
                                  });
                          }

                      }
                  }, function(x) {
                      $scope.authError = '服务器错误！请联系管理员。';
                  });
          }
      }
      // Try to login

    };
  }])
;